const models = require("../db/mysqldb/index");
const { Op } = require("sequelize");

class DataSystemRoleMenuModel {
  /**
   * @description 删除
   * @param {*} role_id
   * @returns
   */
  static async delete({role_id}) {
    return await models.sys_role_menu.destroy(
      {
        where: { role_id },
      }
    );
  }
  /**
   * @description 插入
   * @param {*} data
   * @returns
   */
  static async create({data}) {
    return await models.sys_role_menu.bulkCreate(data);
  }

  /**
   * @description 获取所有
   * @param {*} data
   * @returns
   */
  static async all() {
    return await models.sys_role_menu.findAll();
  }

  /**
   * @description 根据role_id 获取
   * @param {*} data
   * @returns
   */
  static async findPermissionByRoleId({role_ids}) {
    let sql = `SELECT sm.perms as permission FROM sys_role_menu srm LEFT JOIN sys_menu sm 
    on srm.menu_id = sm.menu_id 
    WHERE sm.del_flag = '0' and srm.role_id in (${role_ids}) and status = 1 and sm.perms <>''`;
    return await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
  }
}
module.exports = {
  DataSystemRoleMenuModel,
};
